﻿using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Aliyun.Net.SDK.Core;
using Aliyun.Net.SDK.Core.Exceptions;
using Aliyun.Net.SDK.Core.Profile;
using OddUserManager.Data.Repositories;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Service.Services;
using OddUserManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OddUserManager.Web.Common
{
    public class MsgService
    {
        
        static String product = "Dysmsapi";//短信API产品名称
        static String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
        static String accessId = "LTAIIzdns3SWH2G4";
        static String accessSecret = "bWYX36vUsa5Y2tsaO5kwvSDZqeMwHI";
        static String regionIdForPop = "cn-hangzhou";


        private static MsgService _instance;
        private static object _lock = new object();


        IPhoneMessagesService _service = new PhoneMessagesService(new PhoneMessagesRepository());

        private MsgService() { }

        public static MsgService GetInstance() {
            if (null == _instance)
            {
                lock (_lock)
                {
                    if (null == _instance)
                    {
                        _instance = new MsgService();
                    }
                }
            }
            return _instance;
        }


        public JsonData SendMsg(string msg)
        {
            JsonData d = new JsonData();
            IClientProfile profile = DefaultProfile.GetProfile(regionIdForPop, accessId, accessSecret);
            DefaultProfile.AddEndpoint(regionIdForPop, regionIdForPop, product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            PhoneMessages ss = new PhoneMessages();
            
            try
            {

                //request.SignName = "上云预发测试";//"管理控制台中配置的短信签名（状态必须是验证通过）"
                //request.TemplateCode = "SMS_71130001";//管理控制台中配置的审核通过的短信模板的模板CODE（状态必须是验证通过）"
                //request.RecNum = "13567939485";//"接收号码，多个号码可以逗号分隔"
                //request.ParamString = "{\"name\":\"123\"}";//短信模板中的变量；数字需要转换为字符串；个人用户每个变量长度必须小于15个字符。"
                //SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);
                request.PhoneNumbers = "13302332200,13302338678";
                ss.PhoneNumbers= "13302332200,13302338678";
                ss.Message = "{\"time\":\"" + DateTime.Now.ToShortTimeString() + "\",\"message\":\"" + msg + "\"}";
                ss.SendTime = DateTime.Now;
                request.SignName = "资讯";
                request.TemplateCode = "SMS_135040022";
                request.TemplateParam = "{\"time\":\"" + DateTime.Now.ToShortTimeString() + "\",\"message\":\""+msg+"\"}";
                request.OutId = "1000";
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                d.Message = sendSmsResponse.Message;
                d.Success = true;
                ss.SendCode = sendSmsResponse.Code;
                ss.ReturnMsg = sendSmsResponse.Message;


            }
            catch (ServerException e)
            {
                d.Message = e.Message;
                d.Success = false;
                ss.Message = e.Message;
                ss.SendCode = e.ErrorCode;
            }
            catch (ClientException e)
            {
                d.Message = e.Message;
                d.Success = false;
                ss.Message = e.Message;
                ss.SendCode = e.ErrorCode;
            }

            _service.InsertOrUpdate(ss);

            return d;
        }
    }
}
